The `push` operation is the core function for adding data to a stack, following the FILO principle.
- While the previous slide established boundary checks, the push operation is the mechanism that risks hitting the upper boundary (Overflow), strictly adhering to FILO.
- Boundary Check: First, verify the stack is not full using an `isFull` check (e.g., top == n - 1). If it is full, the push must be rejected to prevent a Stack Overflow error.
- Increment Pointer: If the stack has space, the top index pointer must be incremented *before* the new element is inserted.
- Insert Element: Place the new `value` into the array at the location of the newly incremented top index.
- Remember: The top variable points to the last inserted element. To push, we must move the pointer first, then insert into the newly available slot.
Push Operation Properties
| Property | Description | Complexity |
|---|---|---|
| Pre-condition | Stack is not full ($top < n-1$) | $O(1)$ |
| Pointer Action | Increment `top` index (`top += 1`) | $O(1)$ |
| Data Action | Insert value at new `top` | $O(1)$ |
| Overall | Constant time operation | $O(1)$ |